(function(){
    var factory = {
        init: function(){
            var _this = this;
            _this.scrollLoadingPic();
        },
        /* 图片懒加载 */
        scrollLoadingPic:function () {
            $("img.lazy").lazyload();
        },
        /* tab悬浮切换 */
        switchTab: function(event){
              var _this = this;
              var index = event.currentTarget.getAttribute("data-index");
              $(event.currentTarget).addClass('active').siblings().removeClass('active');
              $("#"+index).removeClass('hide').siblings().addClass('hide');
        },
        /* 选择参数 */
        selectParam: function(event){
              var _this = this;
              $(event.currentTarget).addClass('selected').siblings().removeClass('selected');
        },
        /* 点击星星 */
        rank: function(event){
          var _this = this;
          var redStarList = $(".redstar");
          var index = event.currentTarget.getAttribute("data-index");
          $(event.currentTarget).addClass('score').siblings().removeClass('score');
          _this.reSetStarList(index, redStarList);
        },
        /* 鼠标进入星星 */
        enter: function(event){
            var _this = this;
            var scoreValue;
            var indexWhenLeave;
            var index = event.currentTarget.getAttribute("data-index");
            var redStarList = $(".redstar");
            scoreValue = _this.getScoreValue();
            indexWhenLeave = _this.getIndexWhenLeave();
            if (scoreValue < indexWhenLeave) {
              _this.reSetStarList(scoreValue, redStarList);
            }
            if (index <= scoreValue) return;
            _this.reSetStarList(index, redStarList);
        },
        /* 鼠标离开星星 */
        leave: function(){
          var _this = this;
          var scoreValue;
          var indexWhenLeave;
          var redStarList = $(".redstar");
          scoreValue = _this.getScoreValue();
          if (scoreValue===0) {
            for (var i = 0; i < 5; i++) {
                $(redStarList[i]).addClass('hide').siblings().removeClass('hide');
            }
            return;
          }
          indexWhenLeave = _this.getIndexWhenLeave();
          if (indexWhenLeave<=scoreValue) return;
          _this.reSetStarList(scoreValue, redStarList);
        },
        /* 获取当前设定的红色星星的数量 */
        getScoreValue: function(){
          var scoreValue;
          if ($(".score").length > 0) {
            scoreValue = $(".score").attr("data-index");
          } else {
            scoreValue = 0;
          }
          return scoreValue;
        },
        /* 获取上一个离开的星星的index */
        getIndexWhenLeave: function(){
          var indexWhenLeave;
          if ($(".redstar.hide").length > 0) {
            indexWhenLeave = $($(".redstar.hide")[0]).parent(".item").attr("data-index")-1;
          } else {
            indexWhenLeave = 5;
          }
          return indexWhenLeave;
        },
        /* 重新渲染星星列表 */
        reSetStarList: function(index, redStarList){
          for (var i = 0; i < 5; i++) {
            if (i+1<=index) {
              $(redStarList[i]).removeClass('hide').siblings().addClass('hide');
            } else {
              $(redStarList[i]).addClass('hide').siblings().removeClass('hide');
            }
          }
        }
    };

    module.exports = factory;
})();
